package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.common.util.StringUtils;
import com.mianyang.zhangsan.model.entity.SecTask;
import com.mianyang.zhangsan.plugin.quartz.util.QuartzTaskFactory;
import com.mianyang.zhangsan.service.SecTaskService;
import com.mianyang.zhangsan.mapper.SecTaskMapper;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author jiangjr
 * @date 2022-11-25
*/
@Service
public class SecTaskServiceImpl extends ServiceImpl<SecTaskMapper, SecTask>
    implements SecTaskService{
    @Autowired
    private QuartzTaskFactory taskFactory;
    @Autowired
    private Scheduler scheduler;

    @Override
    public Page<SecTask> selectTaskByPage(SecTask task, Integer current, Integer pageSize) {
        QueryWrapper<SecTask> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(task.getTaskGroup())) {
            wrapper.eq("task_group", task.getTaskGroup());
        }
        if (StringUtils.isNotEmpty(task.getTaskName())) {
            wrapper.like("task_name", task.getTaskName());
        }
        if (StringUtils.isNotEmpty(task.getAccountName())) {
            wrapper.like("account_name", task.getAccountName());
        }
        if (task.getTaskType() != null) {
            wrapper.eq("task_type", task.getTaskType());
        }
        return page(new Page<>(current, pageSize), wrapper);
    }

    @Override
    public void schedulerAndSave(SecTask task) {
        // 保存任务
        save(task);
        // 加入调度器
        taskFactory.addTask(scheduler, task);
    }
}




